package ups.MapParse;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import ups.Common.*;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class ShipManager {
	ArrayList<Ship> ships;
	/* This class handles everything to do with the ShipFinder and TextFinder classes, as well as the database stuff in ShipDatabase. */
	public ShipManager( String[] imageFiles, String hintFile, String letterFolder, String offsetFile, Hashtable<String,String> options ) {
		System.out.println( "cwd = " + System.getProperty( "user.dir"));
		File cwd = new File( System.getProperty( "user.dir"));
		HashMap<String,Integer> offsetMap = loadBaselineFile( new File( cwd, offsetFile ));
		
		ships = new ArrayList<Ship>();
		
		for( String imageFile : imageFiles ) {
			ShipFinder finder = null;
			try {
				//System.out.println( imageFile );
				finder = new ShipFinder( new File( cwd, imageFile ),
											loadPatternsFromDirectory( new File( cwd, letterFolder ), offsetMap ),
											loadPossibleMatches( new File( cwd, hintFile )) );
			}
			catch( Exception e ) {
				System.out.println( "ShipFinder Failed" );
				e.printStackTrace();
			}
			if( finder != null ) {
				for( Ship s : finder.getShips() ) {
					Date fileDate = null;
					try {
						String dateString = imageFile.substring(0,  imageFile.lastIndexOf(".") );
						dateString = dateString.substring( 0, dateString.lastIndexOf("."));
						dateString = dateString.substring( dateString.lastIndexOf("/")+1);
						fileDate = new SimpleDateFormat("yyyy.DDD.HH.mm.ss" ).parse( dateString );//DateFormat.getInstance().parse( dateString );
					//cal.setTime( date );
					} catch( ParseException pe ) {
						pe.printStackTrace();
					}
					s.setDate( fileDate );
				}
				ships.addAll( finder.getShips() );
				//System.out.println( finder.getShips().toString() );
				}
		}
	}
	
	public ArrayList<Ship> getShips() {
		return ships;
	}

	public static ArrayList<LetterPattern> loadPatternsFromDirectory( File directory, HashMap<String, Integer> baselineOffsetMap ) {
		ArrayList<LetterPattern> list = new ArrayList<LetterPattern>();
		for( File letterFile : directory.listFiles() ) {
			if( letterFile.getName().endsWith(".png") || letterFile.getName().endsWith( ".gif" )) {
				/* Load Letter Patterns 2 at a Time, One for Each Color */
				LetterPattern pattern = new LetterPattern( letterFile.toString() );
				pattern.setName( letterFile.getName().substring(0, 1));
				pattern.setSourceFileName( letterFile.getName().replaceAll( ".gif|.png", "") );
				list.add(pattern);
				if( ! baselineOffsetMap.containsKey( pattern.getSourceFileName() )) {
					baselineOffsetMap.put( pattern.getSourceFileName() , new Integer(0));
					pattern.setOffset( 0 );
				} else {
					pattern.setOffset( baselineOffsetMap.get( pattern.getSourceFileName() ));
				}
			}
		}
		
		Collections.sort( list, new LetterPatternComparator() );
		Collections.reverse(list);
		
		return list;
	}
	
	public static HashMap<String, Integer> loadBaselineFile( File baselineFile ) {
		HashMap<String, Integer> baselineOffsetMap = new HashMap<String, Integer>();
		try {
			FileInputStream input = new FileInputStream( baselineFile.toString() );
			byte b[] = new byte[ input.available() ];
			input.read(b);
			input.close();
			String[] baselines = new String(b).split("\n");
			
			for( String s : baselines ) {
				/* This RegEx splits Any number of space characters + = + any number of space */
				String[] keyAndValue = s.split("\\s*=\\s*"); 
				baselineOffsetMap.put( keyAndValue[0], new Integer(keyAndValue[1]));
			}
		} catch ( Exception e ) {
			e.printStackTrace();
		}
		
		return baselineOffsetMap;
	}
	
	public static ArrayList<String> loadPossibleMatches( File matchesFile) {
		ArrayList<String> possibilities = new ArrayList<String>();
		try {
			FileInputStream input = new FileInputStream( matchesFile.toString() );
			byte b[] = new byte[ input.available() ];
			input.read(b);
			input.close();
			String[] baselines = new String(b).split("\n");
			
			for( String k : baselines ) {
				possibilities.add( k );
			}
		} catch ( Exception e ) {
			e.printStackTrace();
		}
		
		return possibilities;
	}
}
